<template>
  <div class="pages">
<!--    <div class="weui-cells__title">意见与建议</div>-->
    <div class="weui-form-preview">


      <form @submit='addMessage'>

        <div class="weui-form__control-area">
          <div class="weui-cells__group weui-cells__group_form">

            <div class="weui-cells weui-cells_form" style="margin-top:0;">
              <div class="weui-cell" >
                <div class="weui-cell__bd">

                  <textarea class="weui-textarea" :value="message" @blur="onInput($event)" placeholder="请输入需要跳转的网址" rows="3"></textarea>

                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="weui-form__opr-area">
          <button class="weui-btn weui-btn_primary" form-type="submit" id="showTooltips">提交</button>
        </div>
      </form>
    </div>
    <!--      <div class="weui-cells__title">上传截图</div>-->

  </div>
</template>
<script>
  export default{
    data(){
      return {
        message:"",
        id:"",
      }
    },
    onShow(){
      wx.setNavigationBarTitle({
        title: '外连接设置'
      })
      this.getDetail();
      // (https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]
    },
    methods:{
      getDetail(){
        let cartId=wx.getStorageSync('cartId');
        let userId = wx.getStorageSync('userId')
        this.$http.request({
          method:"get",
          url:`/api/promotion/detailByCardId`,
          body:{
            cardId:cartId,
            userId:userId
          }
        }).then((data)=>{
          if(data.code==200){
            this.id=data.data.id;
             this.message=data.data.description;
          }else{

          }
        })
      },
      addMessage(){
          console.log(this.message)
        let cartId=wx.getStorageSync('cartId')
        let userId=wx.getStorageSync('userId')
        let test=/((ht|f)tps?:)\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
        if(!this.message) {
          wx.showToast({
            title:'请输入地址' ,
            icon: 'none',
            duration:2000

          })
        }else if(test.test(this.message)){
          if(!this.id){
            this.$http.request({
              method:"post",
              url:"/api/promotion/add",
              body:{
                "cardId": cartId,
                "description": this.message,
                "userId": userId
              }
            }).then((data)=>{
              if(data.code==200){
                wx.showToast({
                  title:'添加成功' ,
                  icon: 'success',
                  duration:2000

                })
              }else{
                wx.showToast({
                  title:'添加失败,请重试!' ,
                  icon: 'none',
                  duration:2000

                })
              }
            })
          }else{
            this.$http.request({
              method:"post",
              url:"/api/promotion/update",
              body:{
                "cardId": cartId,
                "description": this.message,
                "id": this.id,
                userId:userId
              }
            }).then((data)=>{
              if(data.code==200){
                wx.showToast({
                  title:'修改成功' ,
                  icon: 'success',
                  duration:2000

                })
              }else{
                wx.showToast({
                  title:'修改失败,请重试!' ,
                  icon: 'none',
                  duration:2000

                })
              }
            })
          }
        }else{
          wx.showToast({
            title:'请输入正确的地址' ,
            icon: 'none',
            duration:2000

          })
        }


      },
      onInput(e){
        //  console.log(e)
        this.message=e.mp.detail.value;
      },
    }
  }
</script>

<style>

</style>
